package com.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.Calculate;
import com.beans.Staff;
import com.services.CalculateService;
import com.services.SelectService;
import com.services.StaffService;

/**
 * Servlet implementation class CalculateServlet
 */
@WebServlet("/calculate")
public class CalculateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private StaffService staff = new StaffService();
	private CalculateService cal = new CalculateService();
	private SelectService select = new SelectService();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CalculateServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		if (method == null) {
			List<Staff> list = staff.selectAll();
			request.setAttribute("list", list);
			request.getRequestDispatcher("calculate.jsp").forward(request, response);
		} else {
			String month = request.getParameter("method");
			List<Calculate> list = select.selectAllStaff(month);
			request.setAttribute("list", list);
			request.getRequestDispatcher("select.jsp").forward(request, response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		List<Staff> list = staff.selectAll();
		String month = request.getParameter("month");
		String shouldStr = request.getParameter("should");
		int should = Integer.parseInt(shouldStr);
		String[] realityStr = request.getParameterValues("reality");
		String[] overtimeStr = request.getParameterValues("overtime");
		String[] otherStr = request.getParameterValues("other");
		if(method.equals("confirm")) {
			List<Calculate> list2 = new ArrayList<>();
			for (int i = 0; i < list.size(); i++) {
				int reality = should;
				int overtime = 0;
				int other = 0;
				if (realityStr[i].length() > 0) {
					reality = Integer.parseInt(realityStr[i]);
				}
				if (overtimeStr[i].length() > 0) {
					overtime = Integer.parseInt(overtimeStr[i]);
				}
				if (otherStr[i].length() > 0) {
					other = Integer.parseInt(otherStr[i]);
				}
				list2.add(new Calculate(list.get(i).getStaffSno(), list.get(i).getName(),
						list.get(i).getDepartmentName(), reality, overtime, other));

			}
			request.setAttribute("month", month);
			request.setAttribute("should", should);
			request.setAttribute("list", list2);
			request.getRequestDispatcher("confirm.jsp").forward(request, response);
		}

		else {
			cal.init(month);
			for (int i = 0; i < list.size(); i++) {
				int reality = Integer.parseInt(realityStr[i]);
				int overtime = Integer.parseInt(overtimeStr[i]);
				int other = Integer.parseInt(otherStr[i]);
				cal.calculate(month, list.get(i).getDepartmentSno(), list.get(i).getDepartmentName(),
						list.get(i).getStaffSno(), list.get(i).getName(), list.get(i).getPost(), should, reality,
						overtime, other);
			}
			response.sendRedirect("/salary_manage/calculate?method=" + month);
			
		}

	}
}
